import base64
import json
import logging
import decimal
import hmac
import hashlib
import uuid


class DecimalEncoder(json.JSONEncoder):

    def default(self, o):
        if isinstance(o.get('price'), decimal.Decimal):
            return float(o.get('price'))
        super(DecimalEncoder, self).default(o)


def json_decode(data):
    try:
        if isinstance(data, bytes):
            data = data.decode('utf8')
        dic = json.loads(data)
        return dic
    except Exception as e:
        logging.error(e)
        return {}


def json_encode(data):
    try:
        return json.dumps(data, cls=DecimalEncoder, ensure_ascii=False)
    except Exception as e:
        print(e)
        return None


def password_decrypt(password):
    return password


def generate_md5(key):
    return hashlib.md5(base64.b64encode(uuid.uuid4().bytes + key.encode())).hexdigest()

